use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use crate::error::Error;
use crate::mapper::ValueMapper;
use crate::modeling::{Class, ClassAttributeBuilder, ClassBuilder, ClassGetter, ValueType};
use crate::modeling::{
    ClassAttributePersistence, ClassPersistence, ClassPersistenceBuilder, ClassPersistenceGetter,
    ClassRelation, ClassRelationGetter, Value,
};

const WORKFLOW_VARIABLE_CLASS_IDENTIFIER: &'static str = "g01_workflow_variable";

const WORKFLOW_VARIABLE_CLASS_NAME: &'static str = "workflow_variable";

const WORKFLOW_VARIABLE_TABLE_NAME: &'static str = "_g01_meta_workflow_variables";

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum WorkflowVariableType {
    /// Agent
    #[default]
    #[serde(rename = "environment")]
    EnvironmentVariable,
    /// G01Server
    #[serde(rename = "conversation")]
    ConversationVariables,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "WorkflowVariableBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct WorkflowVariable {
    /// id
    pub id: i64,
    /// workflow_identifier
    pub workflow_identifier: String,
    /// 名称
    pub index: u32,
    /// 名称
    pub variable: String,
    /// 名称
    pub variable_type: WorkflowVariableType,
    /// 图标
    pub label: String,
    /// 注释
    pub value_type: String,
    /// 选项
    pub schema: String,
    /// 创建时间
    pub created_at: i64,
    /// 更新时间
    pub updated_at: i64,
}
//
crate::impl_from_value!(WorkflowVariable);
crate::impl_class_persistence_getter!(WorkflowVariable, WORKFLOW_VARIABLE_TABLE_NAME);
crate::impl_class_relations_getter!(WorkflowVariable, vec![]);

impl ClassGetter for WorkflowVariable {
    fn get_name() -> anyhow::Result<String> {
        Ok(String::from(WORKFLOW_VARIABLE_CLASS_NAME))
    }
    fn get_identifier() -> anyhow::Result<String> {
        Ok(String::from(WORKFLOW_VARIABLE_CLASS_IDENTIFIER))
    }
    fn get_class() -> anyhow::Result<Class> {
        let mut attribute_builder = ClassAttributeBuilder::default();
        attribute_builder.class_identifier(WORKFLOW_VARIABLE_CLASS_IDENTIFIER);

        let mut commons = crate::modeling::simple_attributes_with_builder(&attribute_builder)?;
        let mut append = vec![
            attribute_builder
                .clone()
                .name("workflow_identifier")
                .title("供应商")
                .data_type(ValueType::String)
                .build()?,
            attribute_builder
                .clone()
                .name("form_schema_identifier")
                .title("schema")
                .data_type(ValueType::String)
                .build()?,
        ];
        append.append(&mut commons);
        append.sort_by(|a, b| a.position.cmp(&b.position));

        let clazz = ClassBuilder::default()
            .identifier(WORKFLOW_VARIABLE_CLASS_IDENTIFIER)
            .name(WORKFLOW_VARIABLE_CLASS_NAME)
            .data_is_type(true)
            .is_persistent(true)
            .primary_key(String::from("identifier"))
            .attributes(append)
            .build()?;
        Ok(clazz)
    }
}
